﻿    @using eLoan.Models;
@model LoanPaymentModel

@{
    ViewBag.Title = "Loan Payment";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        @if (Model.IsCashPayment)
        {
            <h2>Cash Payment</h2>
        }
        else
        {
            <h2>Cheque Payment</h2>
        }

        <ol class="breadcrumb">
            <li>
                Workspace
            </li>
            <li class="active">
                @if (Model.IsCashPayment)
                {
                    <strong>Cash Payment</strong>
                }
                else
                {
                    <strong>Cheque Payment</strong>
                }

            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
            @using (Html.BeginForm("SaveLoanPayment", "Workspace", FormMethod.Post, new { id = "saveForm" }))
            {
    <fieldset>
        <div class="ibox-title">
            <div class="divSearch">
                <table class="table-search">
                    <tr>
                        <th>
                            <span>@Html.LabelFor(m => m.Branches)</span>
                        </th>
                        <th>
                            <span class="span">@Html.LabelFor(m => m.Agents)</span>
                        </th>
                        <th>
                            <span class="span">@Html.LabelFor(m => m.SearchText)</span>
                        </th>
                        <th></th>
                    </tr>
                    <tr>
                        <td>
                            @Html.DropDownListFor(m => m.SelectedBranchCode, Model.Branches, new { @id = "ddlBranches", @class = "text-box single-line", @width = "50%", @onchange = "GetAgents();" })
                        </td>
                        <td style="padding-left:10px">
                            @Html.DropDownListFor(m => m.SelectedAgentID, Model.Agents, new { @id = "ddlAgents", @class = "text-box single-line", @width = "50%", @onchange = "Refresh()" })
                        </td>
                        <td>
                            @Html.TextBoxFor(m => m.SearchText, new { @id = "txtSearchText", @class = "single-line", @width = "50%" })
                        </td>
                        <td style="vertical-align:bottom">
                            <button type="submit" id="btnSearch" class="btn btn-primary btnSearch" name="command" value="Refresh">Search</button>
                        </td>
                        <td style="vertical-align:bottom">
                            <button type="submit" id="btnExport" class="btn btn-primary btnSearch" name="command" value="Export">Export to excel</button>
                        </td>
                        <td class="align-right">
                            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#confirm-post">Post loan payments</button>
                            <button type="submit" id="btnSave" style="display:none" name="command" value="Save">Post loan payments</button>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="ibox-title">
            <div class="pull-left">
                <table>
                    <tr>
                        <td style="width:25%">
                            @Html.LabelFor(m => m.PaymentDate): &nbsp;&nbsp;
                        </td>
                        <td>
                            @if (Model.IsAdmin)
                            {
                                <div class="form-group" id="pd">
                                    <div class="input-group date">
                                        <span class="input-group-addon">
                                            <i class="fa fa-calendar"></i>
                                        </span>
                                        @Html.TextBoxFor(m => m.PaymentDate, "{0:MM/dd/yyyy}", new { @id = "txtPaymentDate", @class = "form-control" })
                                    </div>
                                </div>
                            }
                            else
                            {
                                @Html.ValueFor(m => Model.PaymentDate, "{0:MMM/dd/yyyy}")
                                @Html.HiddenFor(m => Model.PaymentDate)
                            }

                            @Html.HiddenFor(m => Model.IsCashPayment)

                        </td>
                    </tr>
                </table>
            </div>
            <div class="ibox-tools">
                <h3>Total Payment: &nbsp;&nbsp;&nbsp; <span id="total">0.00</span></h3>
            </div>
        </div>
        <div class="ibox-content">
                <table id="tbl" class="table table-bordered table-List">
                    <thead>
                        <tr>
                            <th style="width:150px">Borrower</th>
                            <th>Amount</th>
                            <th>Outstanding Balance</th>
                            <th>Receivable</th>
                            <th>Total Payment</th>
                            <th>Short / Over</th>
                            <th>Payment Start Date</th>
                            <th>Maturity Date</th>
                            <th>Payment Type</th>
                            <th>Penalty</th>
                            <th>Penalty Balance</th>
                            <th>Contact Numbers</th>
                            <th>Loan Description</th>
                            <th>Loan Status</th>
                        </tr>
                    </thead>
                    <tbody id="tableBody">
                        @for (var i = 0; i < Model.PaymentItems.Count(); i++)
                        {
                        <tr class="trData">
                            <td>
                                @Html.HiddenFor(m => m.PaymentItems[i].DocumentNo)
                                @Html.HiddenFor(m => m.PaymentItems[i].BorrowerName)
                                @Html.ActionLink(Model.PaymentItems[i].BorrowerName, "LoanJournal", "Loan", null, null, null, new { documentNo = Model.PaymentItems[i].DocumentNo }, null)
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.PaymentItems[i].Amount, "{0:#,###,##0.00}", new { @class = "amount  single-line scroll-field1", @onchange = "FormatMe(this); GetTotal();" })
                            </td>
                            <td style="text-align:right">
                                @Html.ValueFor(m => m.PaymentItems[i].OutstandingBalance, "{0:#,###,##0.00}")
                                @Html.HiddenFor(m => m.PaymentItems[i].OutstandingBalance)
                            </td>
                            <td style="text-align:right">
                                @Html.ValueFor(m => m.PaymentItems[i].Receivable, "{0:#,###,##0.00}")
                                @Html.HiddenFor(m => m.PaymentItems[i].Receivable)
                            </td>
                            <td style="text-align:right">
                                @Html.ValueFor(m => m.PaymentItems[i].TotalPayment, "{0:#,###,##0.00}")
                                @Html.HiddenFor(m => m.PaymentItems[i].TotalPayment)
                            </td>
                            <td style="text-align:right">
                                @Html.ValueFor(m => m.PaymentItems[i].ShortOver, "{0:#,###,##0.00}")
                                @Html.HiddenFor(m => m.PaymentItems[i].ShortOver)
                            </td>
                            <td>
                                @Html.ValueFor(m => m.PaymentItems[i].PaymentStartDate, "{0:yyyy/MM/dd}")
                                @Html.HiddenFor(m => m.PaymentItems[i].PaymentStartDate)
                            </td>
                            <td>
                                @Html.ValueFor(m => m.PaymentItems[i].PaymentEndDate, "{0:yyyy/MM/dd}")
                                @Html.HiddenFor(m => m.PaymentItems[i].PaymentEndDate)
                            </td>
                            <td>
                                @Html.DropDownListFor(m => m.PaymentItems[i].PaymentTypeID, new SelectList(Model.PaymentTypes, "Value", "Text", Model.PaymentItems[i].PaymentTypeID), new { @class = "single-line" })
                                @Html.HiddenFor(m => m.PaymentItems[i].PaymentTypeName)
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.PaymentItems[i].Penalty, "{0:#,###,##0.00}", new { @class = "amount  single-line scroll-field2", @onchange = "FormatMe(this); GetTotal();" })
                            </td>
                            <td style="text-align:right">
                                @Html.ValueFor(m => m.PaymentItems[i].PenaltyBalance, "{0:#,###,##0.00}")
                                @Html.HiddenFor(m => m.PaymentItems[i].PenaltyBalance)
                            </td>
                            <td>
                                @Html.ValueFor(m => m.PaymentItems[i].ContactNumbers)
                                @Html.HiddenFor(m => m.PaymentItems[i].ContactNumbers)
                            </td>
                            <td>
                                @Html.HiddenFor(m => m.PaymentItems[i].LoanTypeDescription)
                                @Html.ValueFor(m => m.PaymentItems[i].LoanTypeDescription)
                            </td>
                            <td>
                                @Html.ValueFor(m => m.PaymentItems[i].LoanStatus)
                                @Html.HiddenFor(m => m.PaymentItems[i].LoanStatus)
                            </td>
                        </tr>
                        }
                    </tbody>
                </table>

            <div class="modal fade" id="confirm-post" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">

                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title" id="myModalLabel">Confirmation</h4>
                        </div>

                        <div class="modal-body">
                            <p>Are you sure you want to post the encoded payments?</p>
                            <p class="debug-url"></p>
                        </div>

                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary" data-dismiss="modal">No</button>
                            <button type="button" class="btn btn-primary btn-ok" data-dismiss="modal">Yes</button>
                        </div>
                    </div>
                </div>
            </div>

            <button type="submit" id="btnRefresh" style="display:none" name="command" value="Refresh">Refresh</button>

        </div>
    </fieldset>
            }
            </div>

        </div>
    </div>
</div>

@section Styles {
@Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")
@Styles.Render("~/plugins/dataPickerStyles")
    <style>

    .table-search {
        width: 100%;
        border: none;
    }

        .table-search th {
            width: 250px;
        }

            .table-search th label {
                margin-top: 15px;
                margin-left: 20px;
            }

            .table-search th .span {
                padding-left: 10px;
            }

        .table-search .align-right{
            text-align: right;
        }

            .table-search td select {
                margin-left: 15px !important;
                margin-bottom: 10px !important;
            }

            .table-search td input {
                margin-left: 25px !important;
                margin-bottom: 10px !important;
            }

            .table-search td button {
                margin-right: 15px;
                margin-bottom: 10px !important;
            }

        .table-search td .btnSearch {
            margin-bottom: 0px;
            margin-left: 40px !important;
        }

    .divSearch {
        background-color: #F5F5F6;
        border: 1px solid #EBEBEB;
    }

</style>

}

@section Scripts {
@Scripts.Render("~/plugins/dataTables")
@Scripts.Render("~/plugins/dataPicker")

<script type="text/javascript">

    $(document).ready(function () {

        $('.table-List').dataTable({
            ordering: true,
            searching: false,
            paging: false,
            scrollY: 400,
            scrollX: true
        });
        
        $(window).keydown(function (event) {
            if (event.keyCode == 13) {
                Refresh();
                return false;
            }
        });

        $('#confirm-post').on('click', '.btn-ok', function (e) {
            document.getElementById('btnSave').click();
        });

        SetDateFormat();
    });

    function Refresh() {
        document.getElementById('btnRefresh').click();
    }

    function GetTotal()
    {
        var totalAmount = 0;
        $('.amount').each(function () {
            var val = $(this).val().replace(',','');
            
            totalAmount = parseFloat(totalAmount) + parseFloat(val);

        });

        var total = $('#total');
        total.text(FormatNumber(totalAmount.toFixed(2)));
    }

    function FormatNumber(nStr) {
        nStr += '';
        var x = nStr.split('.');
        var x1 = x[0];
        var x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    }

    function FormatMe(me) {
        var val = $(me).val().replace(',', '').replace(',', '').replace(',', '');

        if (isNaN(parseFloat(val)))
            val = 0.00;

        var decimalFormat = parseFloat(val).toFixed(2);
        var commaFormat = FormatNumber(decimalFormat);

        $(me).val(commaFormat);
    }

    function GetAgents()
    {
        var val = $('#ddlBranches').val();
        var subItems = "";

        $.getJSON("@Url.Action("GetAgents","Borrower")", { branchCode: val },
        function (data) {

            subItems += "<option value='0'>All</option>"
            $.each(data, function (index, item) {
                subItems += "<option value='" + $.trim(item.Value) + "'>" + $.trim(item.Text) + "</option>"
            });

            $("#ddlAgents").html(subItems)
    });
    }

    function SetDateFormat() {

        $('#pd .input-group.date').datepicker({
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            format: 'mm/dd/yyyy',
            autoclose: true

        });

        var pd = $('#txtPaymentDate').val();
        $('#pd .input-group.date').datepicker("update", new Date(pd));

    }

</script>

}
